<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit || disableSave">
      <el-row>
        <el-col :span="24">
          <el-divider content-position="left"><span>1、【沟通计划】相关信息</span></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目" prop="projectid" :rules="rules.validateRequire">
            <el-input v-model="model.dispprojectid" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmProjectRefDialog" v-if="!disableEditSubmit || disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="沟通类别" prop="category" :rules="rules.validateRequire">
            <el-select v-model="model.category" filterable clearable>
              <el-option v-for="item in codeModel.categoryList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="计划人" prop="plannername" :rules="rules.validateRequire">
            <el-input v-model="model.plannername" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openPlannerpmTeamRefDialog" v-if="!disableEditSubmit || disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划人工号" prop="plannercode" :rules="rules.validateRequire">
            <el-input v-model="model.plannercode" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="负责人" prop="chargename" :rules="rules.validateRequire">
            <el-input v-model="model.chargename" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openChargepmTeamRefDialog" v-if="!disableEditSubmit || disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人工号" prop="chargecode" :rules="rules.validateRequire">
            <el-input v-model="model.chargecode" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="确认人" prop="confirmbyname" :rules="rules.validateRequire">
            <el-input v-model="model.confirmbyname" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openConfirmpmTeamRefDialog" v-if="!disableEditSubmit || disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="确认人工号" prop="confirmbycode" :rules="rules.validateRequire">
            <el-input v-model="model.confirmbycode" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="计划沟通日期" prop="plantime" :rules="rules.validateRequire">
            <el-date-picker v-model="model.plantime" type="date" placeholder="计划沟通日期" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划沟通地点" prop="planplace" :rules="rules.validateRequire">
            <el-input v-model="model.planplace"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="计划沟通内容" prop="plancontent" :rules="rules.validateRequire">
            <el-input v-model="model.plancontent" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row>
                <el-col :span="24">
                    <el-form-item label="计划沟通名称" prop="planname" :rules="rules.validateRequire">
                        <el-input v-model="model.planname" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row> -->
      <el-row>
        <el-col :span="24">
          <el-form-item label="计划参与人" prop="planparticipants">
            <el-input v-model="model.planparticipants" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-divider content-position="left"><span>2、【沟通执行】相关信息</span></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="执行日期" prop="executiontime">
            <el-date-picker v-model="model.executiontime" type="date" placeholder="执行日期" value-format="yyyy-MM-dd" :disabled="true"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="实际参与人" prop="actualparticipants">
            <el-input v-model="model.actualparticipants" type="textarea" :rows="2" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="执行情况" prop="implementation">
            <el-input v-model="model.implementation" type="textarea" :rows="2" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-divider content-position="left"><span>3、【沟通审批】相关信息</span></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="确认日期" prop="confirmtime">
            <el-date-picker v-model="model.confirmtime" type="date" placeholder="确认日期" value-format="yyyy-MM-dd" :disabled="true"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="确认结果" prop="confirmresult">
            <el-input v-model="model.confirmresult" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-collapse>
        <el-collapse-item title="【单据】相关信息">
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请公司名称" prop="orgname" :rules="rules.validateRequire">
                <el-input v-model="model.orgname" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请部门" prop="deptname" :rules="rules.validateRequire">
                <tl-ref-dept v-model="model.deptname" :initDisplayData.sync="model.deptname" dataType="code" nameType="fullname" :disabled="true" :singleSelect="true" @confirm="onSelectDept" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请人姓名" prop="username" :rules="rules.validateRequire">
                <el-input v-model="model.username" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请人工号" prop="usercode" :rules="rules.validateRequire">
                <el-input v-model="model.usercode" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="单据日期" prop="docdate">
                <el-date-picker v-model="model.docdate" type="date" placeholder="单据日期" value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="提交日期" prop="submitdate">
                <el-date-picker v-model="model.submitdate" type="date" placeholder="提交日期" value-format="yyyy-MM-dd" :readonly="true" class="is-disabled"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="审批流程" prop="flowid">
                <el-input v-model="model.dispflowid" :readonly="true" class="is-disabled"> </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态" prop="state">
                <el-input v-model="model.dispstate" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>
    </el-form>
    <tl-approval-history ref="history" sourceType="PmCommunicationManage" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disableSave">
        <svg-icon icon-class="save" />保存
      </el-button>
      <tl-attachment sourceType="PmCommunicationManage" :sourceId="model.id" :disabled="disableAttachmentButton" btnTitle="相关文件"></tl-attachment>
      <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval">
        <svg-icon icon-class="send" />提交审批
      </el-button>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
        <svg-icon icon-class="list" />审批历史
      </el-button>
      <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableDelApproval" :disabled="disableDelApproval">
        <svg-icon icon-class="undo" />取消审批
      </el-button>
    </div>
    <tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true"></tlpm-project-ref>
    <tlpm-team-ref ref="pmTeamRefPlanner" @confirm="getPlannerpmTeamRefData" :isSingleSelect="true" :externalQueryParam="{ qry_projectid: model.projectid }"></tlpm-team-ref>
    <tlpm-team-ref ref="pmTeamRefCharge" @confirm="getChargepmTeamRefData" :isSingleSelect="true" :externalQueryParam="{ qry_projectid: model.projectid }"></tlpm-team-ref>
    <tlpm-team-ref ref="pmTeamRefConfirm" @confirm="getConfirmpmTeamRefData" :isSingleSelect="true" :externalQueryParam="{ qry_projectid: model.projectid }"></tlpm-team-ref>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlpmProjectRef from '@/views/ProjectManage/pmProject/refList.vue'
import tlpmTeamRef from '@/views/ProjectManage/pmTeam/refList.vue'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { tlpmProjectRef, tlpmTeamRef },
  data() {
    return {
      moduleTitle: '沟通管理',
      urlPrefix: '/pm/communicationManage/',
      disableAttachmentButton: true,
      url: {
        // 额外的url
        queryOrgByDeptId: '/pm/resourceArchives/queryOrgByDeptId'
      }
    }
  },
  props: {
    projectId: {
      type: [Number, String],
      default: function() {
        return ''
      }
    },
    editFlag: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    assignDefaultValue() {
      if (this.isAdd) {
        // if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
        //   this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        // }
        // this.$set(this.model, 'orgcode', this.org.code)
        // this.$set(this.model, 'orgname', this.org.name)
        // this.$set(this.model, 'deptcode', this.dept.code)
        // this.$set(this.model, 'deptname', this.dept.fullname)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'docdate', new Date())
        this.$set(this.model, 'plannername', this.user.name)
        this.$set(this.model, 'plannercode', this.user.code)
      }
    },
    afterDelApproval() {
      this.disableAttachmentButton = false
    },
    loaded() {
      if (this.isAdd === false && this.disableEditSubmit === true) {
        var params = {
          sourceType: 'PmCommunicationManage',
          sourceId: this.model.id
        }
        this.disableAttachmentButton = false
        getAction('/cbo/attachment/calculateCount', params).then((res) => {
          if (res.success) {
            if (res.result === 0) {
              this.disableAttachmentButton = true
              this.$emit('refresh')
            }
          }
        })
      }
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
    },
    /** 执行新增提交 */
    doAdd() {
      this.saveLoading = true
      // 增加唯一单号
      this.$set(this.model, 'billuid', this.uid)
      postAction(this.url.add, this.model)
        .then(res => {
          if (res.success) {
            this.$tap.message('添加成功', 'success')
            this.queryData(res.result.id)
            this.disableAttachmentButton = false
            this.isAdd = false
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch(err => {
          this.saveLoading = false
        })
    },
    onSelectDept(selectedDepts) {
      const selectedDept = selectedDepts[0]
      const param = {
        deptId: selectedDept.id
      }
      getAction(this.url.queryOrgByDeptId, param).then(res => {
        if (res.success) {
          const { code: orgCode, name: orgName } = res.result
          this.$set(this.model, 'orgcode', orgCode)
          this.$set(this.model, 'orgname', orgName)
        } else {
          this.$tap.alert('已选部门关联公司信息错误，请联系系统管理员')
        }
      })
    },
    checkSelectProject() {
      if (!this.model.projectid) {
        this.$tap.alert('请先选择项目')
        return false
      }
      return true
    },
    /** 打开项目参照 */
    openpmProjectRefDialog() {
      this.$refs.pmProjectRef.visible = true
    },
    /** 获取项目参数数据 */
    getpmProjectRefData(selectionRow, selectKey, selectionValue) {
      const { id, name } = selectionRow
      this.$set(this.model, 'projectid', id)
      this.$set(this.model, 'dispprojectid', name)
      this.$set(this.model, 'deptcode', selectionRow.ownerdeptcode)
      this.$set(this.model, 'deptname', selectionRow.ownerdeptname)
      this.$set(this.model, 'orgcode', selectionRow.ownerorgcode)
      this.$set(this.model, 'orgname', selectionRow.ownerorgname)
    },
    /** 打开负责人姓名参照 */
    openChargepmTeamRefDialog() {
      if (this.checkSelectProject()) {
        this.$refs.pmTeamRefCharge.visible = true
      }
    },
    /** 获取负责人姓名参数数据 */
    getChargepmTeamRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'chargename', selectionRow.username)
      this.$set(this.model, 'chargecode', selectionRow.usercode)
    },
    /** 打开确认人参照 */
    openConfirmpmTeamRefDialog() {
      if (this.checkSelectProject()) {
        this.$refs.pmTeamRefConfirm.visible = true
      }
    },
    /** 获取确认人参数数据 */
    getConfirmpmTeamRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'confirmbyname', selectionRow.username)
      this.$set(this.model, 'confirmbycode', selectionRow.usercode)
    },
    /** 打开负责人姓名参照 */
    openPlannerpmTeamRefDialog() {
      if (this.checkSelectProject()) {
        this.$refs.pmTeamRefPlanner.visible = true
      }
    },
    /** 获取负责人姓名参数数据 */
    getPlannerpmTeamRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'plannername', selectionRow.username)
      this.$set(this.model, 'plannercode', selectionRow.usercode)
    },
    /** 重写提交审批方法*/
    doSubmitApproval() {
      if (!this.url.submit) {
        this.$tap.alert('请设置审批地址!', 'warning')
        return
      }
      this.submitLoading = true
      this.$tap
        .confirm('确定提交审批吗？提交后，单据内容不可修改！')
        .then(_ => {
          if (!this.beforeApproval()) {
            return
          }
          putAction(this.url.submit, this.model).then(res => {
            if (res.success) {
              this.$tap.message(res.message, 'success')
              this.queryData(this.model.id)
              this.afterApproval()
            } else {
              this.$tap.alert(res.message, 'warning')
            }
            this.submitLoading = false
          })
        })
        .catch(_ => {
          this.submitLoading = false
        })
    }
  }
}
</script>
